package com.cqs.leetcode.dp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lixqw
 * @date 2020/9/5
 */
public class IncreasingTripletSubsequence334 {


    /**
     * 　DP
     * 70MS
     * 是求最长子序列的一个特例
     *
     * @param nums
     * @return
     */
    public boolean increasingTriplet0(int[] nums) {
        if (nums.length < 3) return false;
        //状态dp[i]表示num[i]结尾的递增子序列最大长度
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);
        for (int i = 1; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                    if (dp[i] >= 3) return true;
                }
            }
        }
        return false;
    }

    public boolean increasingTriplet(int[] nums) {
        int min = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= min) {
                min = nums[i];
            } else if (nums[i] <= mid ) {
                mid = nums[i];
            } else if (nums[i] > mid){
                System.out.println(min + "\t" + mid +"\t" + nums[i]);
                return true;
            }
        }
        return false;
    }


    public static void main(String[] args) {
        List<int[]> tests = new ArrayList<>();
        tests.add(new int[]{2,4,-2,-3});
        IncreasingTripletSubsequence334 ic = new IncreasingTripletSubsequence334();

        tests.forEach(it->{

            boolean b = ic.increasingTriplet(it);
            System.out.println(b);
        });
    }
}
